package com.wpzf.ndhc.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wpzf.common.core.utils.ServletUtils;
import com.wpzf.common.core.web.domain.AjaxResult;
import com.wpzf.common.security.service.TokenService;
import com.wpzf.ndhc.service.WpzfNdhcDsfjdService;
import com.wpzf.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wpzfndhcdsfjd")
public class WpzfNdhcDsfjdController {

    @Autowired
    private WpzfNdhcDsfjdService wpzfNdhcDsfjdServiceImpl;

    @Autowired
    private TokenService tokenService;

    //pch
    @RequestMapping(value = "/getPch", method = RequestMethod.POST)
    public AjaxResult getPch(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("user_name", loginUser.getSysUser().getUserName());
        List<Map<String, Object>> rest = wpzfNdhcDsfjdServiceImpl.getPch(param);
        return AjaxResult.success("获取批次号", rest);
    }

    //筛选条件查询接口（省、县）
    @RequestMapping(value = "/getSheng", method = RequestMethod.POST)
    public AjaxResult getSheng(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("user_name", loginUser.getSysUser().getUserName());
        List<Map<String, Object>> rest = wpzfNdhcDsfjdServiceImpl.getSheng(param);
        return AjaxResult.success("获取省", rest);
    }

    @RequestMapping(value = "/getXian", method = RequestMethod.POST)
    public AjaxResult getXian(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("user_name", loginUser.getSysUser().getUserName());
        List<Map<String, Object>> rest = wpzfNdhcDsfjdServiceImpl.getXian(param);
        return AjaxResult.success("获取县", rest);
    }

    //查询地块列表接口
    @RequestMapping(value = "/queryDkTask", method = RequestMethod.POST)
    public AjaxResult queryDkTask(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        param.put("user_name", loginUser.getSysUser().getUserName());
        List<Map<String, Object>> rest = wpzfNdhcDsfjdServiceImpl.queryDkTask(param);
        Integer cnt = wpzfNdhcDsfjdServiceImpl.queryDkTaskCount(param);
        return AjaxResult.success("地块列表", rest, cnt);
    }


    /*保存核查结果*/
    @Transactional
    @RequestMapping(value = "/addHcjg", method = RequestMethod.POST)
    public AjaxResult addHcjg(@RequestBody Map<String, Object> param) {
        try {
            LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
            List<Map<String, Object>> list = new ArrayList<>();
            String selectList = param.get("listParams").toString();
            int rest = 0;
            String ywhj = param.get("ywhj") + "";
            if (!selectList.equals("") && !selectList.equals("null") && selectList != null) {
                JSONObject obj = JSON.parseObject(selectList);
                if (obj != null) {
                    param.put("user_name", loginUser.getUsername());
                    param.put("nick_name", loginUser.getSysUser().getNickName());
                    param.put("dwmc", loginUser.getSysUser().getDept().getDeptName());

                    param.put("sfhc", "是");
                    String strPdlx = param.get("pdlx") + "";

                    if (strPdlx.equals("已办理建设用地审批手续")) {
                        param.put("jl_jlpf", param.get("hf_jlJlpf"));
                    } else if (strPdlx.equals("应办理建设用地审批手续而未办理")) {
                        param.put("jl_jlpf", param.get("wf_jlJlpf"));
                    } else if (strPdlx.equals("其他")) {
                        param.put("jl_jlpf", param.get("qt_jlJlpf"));
                    }

                    String state = param.get("state") + "";
                    Map<String, Object> item = wpzfNdhcDsfjdServiceImpl.queryDsfjdTbhcjg(param);
                    if (item != null) {
                        wpzfNdhcDsfjdServiceImpl.deleteDsfjdTbhcjg(param);
                        wpzfNdhcDsfjdServiceImpl.deleteDsfjdTbhcjgHfDtPwwj(param);
                        wpzfNdhcDsfjdServiceImpl.deleteDsfjdTbhcjgWfpzwh(param);
                        wpzfNdhcDsfjdServiceImpl.deleteDsfjdTbhcjgWftgzmwj(param);
                        wpzfNdhcDsfjdServiceImpl.deleteDsfjdTbhcjgQtpzwh(param);
                        wpzfNdhcDsfjdServiceImpl.deleteDsfjdTbhcjgQtsftgxxydss(param);
                        wpzfNdhcDsfjdServiceImpl.deleteDsfjdTbhcjgQtsfzygd(param);
                    }
                    rest = wpzfNdhcDsfjdServiceImpl.addDsfjdTbhcjg(param);

                    String domains = param.get("domains") + "";
                    if (!domains.equals("[]") && !domains.equals("null") && domains != "null") {
                        JSONArray arraylist = JSONObject.parseArray(domains);

                        for (int i = 0; i < arraylist.size(); i++) { //遍历输出
                            JSONObject jsonObject = arraylist.getJSONObject(i);

                            if (strPdlx.equals("已办理建设用地审批手续")) {
                                Map<String,Object> map = new HashMap<>();
                                map.put("dkbsm", param.get("dkbsm"));
                                map.put("dkbh", param.get("dkbh"));
                                map.put("xzqdm", param.get("xzqdm"));
                                map.put("pch", param.get("pch"));
                                map.put("sort", jsonObject.getString("sort"));
                                map.put("hf_zlpwlx1", jsonObject.getString("hf_zlpwlx1"));
                                map.put("hf_pzwh", jsonObject.getString("hf_pzwh"));
                                map.put("hf_pzxmmc", jsonObject.getString("hf_pzxmmc"));
                                map.put("hf_pzzygdmj", jsonObject.getString("hf_pzzygdmj"));
                                map.put("hf_pzzygdmjdw", jsonObject.getString("hf_pzzygdmjdw"));
                                map.put("hf_pzsj", jsonObject.getString("hf_pzsj"));
                                map.put("hf_zlpwlx2", jsonObject.getString("hf_zlpwlx2"));
                                map.put("hf_zlpwclmc", jsonObject.getString("hf_zlpwclmc"));
                                map.put("ywhj", param.get("ywhj"));
                                wpzfNdhcDsfjdServiceImpl.addDsfjdTbhcjgHfDtPwwj(map);
                            } else if (strPdlx.equals("应办理建设用地审批手续而未办理")) {
                                if (jsonObject.getString("wf_zlpwlx1") != null || jsonObject.getString("wf_zlpwlx2") != null) {
                                    Map<String,Object> map = new HashMap<>();
                                    map.put("dkbsm", param.get("dkbsm"));
                                    map.put("dkbh", param.get("dkbh"));
                                    map.put("xzqdm", param.get("xzqdm"));
                                    map.put("pch", param.get("pch"));
                                    map.put("sort", jsonObject.getString("sort"));
                                    map.put("wf_zlpwlx1", jsonObject.getString("wf_zlpwlx1"));
                                    map.put("wf_pzwh", jsonObject.getString("wf_pzwh"));
                                    map.put("wf_pzxmmc", jsonObject.getString("wf_pzxmmc"));
                                    map.put("wf_pzzygdmj", jsonObject.getString("wf_pzzygdmj"));
                                    map.put("wf_pzzygdmjdw", jsonObject.getString("wf_pzzygdmjdw"));
                                    map.put("wf_pzsj", jsonObject.getString("wf_pzsj"));

                                    map.put("wf_zlpwlx2", jsonObject.getString("wf_zlpwlx2"));
                                    map.put("wf_zlpwclmc", jsonObject.getString("wf_zlpwclmc"));
                                    map.put("ywhj", param.get("ywhj"));
                                    wpzfNdhcDsfjdServiceImpl.addDsfjdTbhcjgWftgzmwj(map);
                                } else {
                                    Map<String,Object> map = new HashMap<>();
                                    map.put("dkbsm", param.get("dkbsm"));
                                    map.put("dkbh", param.get("dkbh"));
                                    map.put("xzqdm", param.get("xzqdm"));
                                    map.put("pch", param.get("pch"));
                                    map.put("sort", jsonObject.getString("sort"));
                                    map.put("wf_pzwh", jsonObject.getString("wf_pzwh"));
                                    map.put("wf_pzxmmc", jsonObject.getString("wf_pzxmmc"));
                                    map.put("wf_pzsj", jsonObject.getString("wf_pzsj"));
                                    map.put("ywhj", param.get("ywhj"));
                                    wpzfNdhcDsfjdServiceImpl.addDsfjdTbhcjgWfpzwh(map);
                                }
                            } else if (strPdlx.equals("其他")) {
                                if (jsonObject.getString("qt_pzwh") != null) {
                                    Map<String,Object> qtpzwh_map = new HashMap<>();
                                    qtpzwh_map.put("dkbsm", param.get("dkbsm"));
                                    qtpzwh_map.put("dkbh", param.get("dkbh"));
                                    qtpzwh_map.put("xzqdm", param.get("xzqdm"));
                                    qtpzwh_map.put("pch", param.get("pch"));
                                    qtpzwh_map.put("sort", jsonObject.getString("sort"));
                                    qtpzwh_map.put("qt_pzwh", jsonObject.getString("qt_pzwh"));
                                    qtpzwh_map.put("qt_pzxmmc", jsonObject.getString("qt_pzxmmc"));
                                    qtpzwh_map.put("qt_pzsj", jsonObject.getString("qt_pzsj"));
                                    qtpzwh_map.put("ywhj", param.get("ywhj"));
                                    wpzfNdhcDsfjdServiceImpl.addDsfjdTbhcjgQtpzwh(qtpzwh_map);
                                } else if (jsonObject.getString("qt_sftgxxydssclmc") != null) {
                                    Map<String,Object> qtsftgxxydss_map = new HashMap<>();
                                    qtsftgxxydss_map.put("dkbsm", param.get("dkbsm"));
                                    qtsftgxxydss_map.put("dkbh", param.get("dkbh"));
                                    qtsftgxxydss_map.put("xzqdm", param.get("xzqdm"));
                                    qtsftgxxydss_map.put("pch", param.get("pch"));
                                    qtsftgxxydss_map.put("sort", jsonObject.getString("sort"));

                                    qtsftgxxydss_map.put("qt_sftgxxydssclmc", jsonObject.getString("qt_sftgxxydssclmc"));
                                    qtsftgxxydss_map.put("qt_sftgxxydsspfsj", jsonObject.getString("qt_sftgxxydsspfsj"));
                                    qtsftgxxydss_map.put("ywhj", param.get("ywhj"));
                                    wpzfNdhcDsfjdServiceImpl.addDsfjdTbhcjgQtsftgxxydss(qtsftgxxydss_map);
                                } else if (jsonObject.getString("qt_tgbawjsj") != null) {
                                    Map<String,Object> qtsfzygd_map = new HashMap<>();
                                    qtsfzygd_map.put("dkbsm", param.get("dkbsm"));
                                    qtsfzygd_map.put("dkbh", param.get("dkbh"));
                                    qtsfzygd_map.put("xzqdm", param.get("xzqdm"));
                                    qtsfzygd_map.put("pch", param.get("pch"));
                                    qtsfzygd_map.put("sort", jsonObject.getString("sort"));
                                    qtsfzygd_map.put("qt_tgbawjsj", jsonObject.getString("qt_tgbawjsj"));
                                    qtsfzygd_map.put("qt_clmc", jsonObject.getString("qt_clmc"));
                                    qtsfzygd_map.put("ywhj", param.get("ywhj"));
                                    wpzfNdhcDsfjdServiceImpl.addDsfjdTbhcjgQtsfzygd(qtsfzygd_map);
                                }
                            }
                        }
                    }

                    Map<String, Object> model = new HashMap();
                    model.put("user_name", loginUser.getUsername());
                    model.put("dwmc", loginUser.getSysUser().getDept().getDeptName());
                    model.put("dkbsm", param.get("dkbsm"));
                    model.put("dkbh", param.get("dkbh"));
                    model.put("xzqdm", obj.getString("xzqdm"));
                    model.put("pch", obj.getString("pch"));
                    model.put("pdlx", obj.getString("pdlx"));
                    model.put("sfhc", obj.getString("sfhc"));
                    model.put("sftj", obj.getString("sftj"));
                    model.put("keyword", obj.getString("keyword"));
                    model.put("ids", param.get("ids"));

                    list = wpzfNdhcDsfjdServiceImpl.queryDkTaskByIndex(model);
                }
            }
            return AjaxResult.success("保存核查结果", list, rest);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("保存失败", e.getMessage());
        }
    }

    /*获取上一个图斑*/
    @RequestMapping(value = "/get_last_tbbh", method = RequestMethod.POST)
    public AjaxResult get_last_tbbh(@RequestBody Map<String, Object> param) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String ywhj = param.get("ywhj") + "";
        String state = param.get("state") + "";
        List<Map<String, Object>> list = new ArrayList<>();
        String selectList = param.get("listParams").toString();
        if (!selectList.equals("") && !selectList.equals("null") && selectList != null) {
            Map<String, Object> model = new HashMap<>();
            JSONObject obj = JSON.parseObject(selectList);
            if (obj != null) {
                model.put("user_name", loginUser.getUsername());
                model.put("nick_name", loginUser.getSysUser().getNickName());
                model.put("dwmc", loginUser.getSysUser().getDept().getDeptName());
                model.put("dkbsm", param.get("dkbsm"));
                model.put("dkbh", param.get("dkbh"));
                model.put("xzqh", obj.getString("xzqh"));
                model.put("xzqdm", obj.getString("xzqdm"));
                model.put("pch", obj.getString("pch"));
                model.put("pdlx", obj.getString("pdlx"));
                model.put("sfhc", obj.getString("sfhc"));
                model.put("sftj", obj.getString("sftj"));
                model.put("keyword", obj.getString("keyword"));
                model.put("ids", param.get("ids"));

                list = wpzfNdhcDsfjdServiceImpl.queryDkTaskByIndex(model);
            }
        }
        return AjaxResult.success("切换地块", list);
    }


}
